package com.yumeng.ui.config;

import com.yumeng.common.utils.PathUtils;
import com.yumeng.config.BaseConfiguration;
import com.yumeng.config.properties.YumengAuthProperties;
import jakarta.annotation.Resource;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties;
import org.thymeleaf.spring6.view.ThymeleafViewResolver;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wxd
 * @date 2024/11/8 16:49
 */
public abstract class ThymeleafConfiguration extends BaseConfiguration {

    private final WebMvcProperties webMvcProperties;
    private final YumengAuthProperties yumengAuthProperties;

    protected ThymeleafConfiguration(WebMvcProperties webMvcProperties, YumengAuthProperties yumengAuthProperties) {
        this.webMvcProperties = webMvcProperties;
        this.yumengAuthProperties = yumengAuthProperties;
    }

    @Resource
    public void configureThymeleafStaticVars(ThymeleafViewResolver viewResolver){
        if(viewResolver != null) {
            String statPath = PathUtils.getStaticPath(webMvcProperties);
            Map<String, Object> vars = new HashMap<>();
            vars.put("staticPath", statPath);
            vars.put("csrfCookieName", yumengAuthProperties.getBasic().getCsrfCookieName());
            viewResolver.setStaticVariables(vars);
        }
    }

}
